//needs full update
package org.amse.tanks.view;

import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

import org.amse.tanks.model.Model;
import org.amse.tanks.objects.Bonus;
import org.amse.tanks.objects.Direction;
import org.amse.tanks.objects.Shot;
import org.amse.tanks.objects.Tank;

public class TextView extends JFrame implements View {
    private Model myModel;

    public TextView(Model model) {
        myModel = model;
    }

    public void update(Model model) {
        if (model != null) {
            myModel = model;
        }
        showTextView();
    }

    public void showTextView() {
        int[][] fullMap;
        int currentValue;

        if (myModel.getMapWidth() == 0) {
            System.out.println("Map is not exist");
            return;
        }

    //build fullMap
        fullMap = createBuffer();

    //show all
        System.out.println();
        for (int k = 0; k < fullMap.length + 2; k++) {          //top border
            System.out.print('#');
        }

        System.out.println();
        for (int i = 0; i < fullMap[0].length; i++) {
            System.out.print('#');                              //left border
            for (int j = 0; j < fullMap.length; j++) {          //map
                currentValue = fullMap[j][i];
                if (currentValue == 0) {    //if empty
                    System.out.print(' ');
                } else {
                    if (currentValue == 1) {    //if wall
                        System.out.print('0');
                    } else {
                        if (currentValue == 2) {    //if bonus
                            System.out.print('@');
                        } else {
                            if (currentValue == 3) {    //if shot
                                System.out.print('+');
                            } else {
                                System.out.print(currentValue); // if tank
                            }
                        }
                    }
                }
            }
            System.out.print('#');                              //right border
            System.out.println();
        }

        for (int k = 0; k < myModel.getMapWidth() + 2; k++) {   //down border
            System.out.print('#');
        }
        System.out.println();
    }

    private int[][] createBuffer() {
        int[][] buffer = new int[myModel.getMapWidth()][myModel.getMapHeight()];
        Iterator iterator;
        Tank tank;
        Shot shot;
        Bonus bonus;
        Direction tankDirection;
        int tankId;
        int tankX;
        int tankY;

        for (int i = 0; i < myModel.getMapHeight(); i++) {      //copying map
            for (int j = 0; j < myModel.getMapWidth(); j++) {
                buffer[j][i] = myModel.getMapValue(j, i);
            }
        }

        if (buffer.length == 0) {
            return buffer;
        }

        iterator = myModel.getTanksList().iterator();           //putting tanks
        while (iterator.hasNext()) {
            tank = (Tank) iterator.next();

            tankDirection = tank.getDirection();
            tankId = tank.getId();
            tankX = tank.getX();
            tankY = tank.getY();

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    buffer[tankX + j][tankY + i] = tankId;
                }
            }

            if (tankDirection == Direction.UP) {
                buffer[tankX + 1][tankY] = 0;
            } else {
                if (tankDirection == Direction.LEFT) {
                    buffer[tankX][tankY + 1] = 0;
                } else {
                    if (tankDirection == Direction.RIGHT) {
                        buffer[tankX + 2][tankY + 1] = 0;
                    } else {
                        if (tankDirection == Direction.DOWN) {
                            buffer[tankX + 1][tankY + 2] = 0;
                        }
                    }
                }
            }
        }

        iterator = myModel.getShotsList().iterator();       //putting shots
        while (iterator.hasNext()) {
            shot = (Shot) iterator.next();
            buffer[shot.getX()][shot.getY()] = 3;
        }

        iterator = myModel.getBonusesList().iterator();       //putting bonuses
        while (iterator.hasNext()) {
            bonus = (Bonus) iterator.next();
            buffer[bonus.getX()][bonus.getY()] = 2;
        }

        return buffer;
    }

    public void showMap() {
        for (int i = 0; i < myModel.getMapHeight(); i++) {
            for (int j = 0; j < myModel.getMapWidth(); j++) {
                System.out.print(myModel.getMapValue(j, i));
            }
            System.out.println();
        }
    }

    public void showTanks(ArrayList tanks) {
        Iterator iterator = tanks.iterator();

        System.out.println("Tanks:");
        while (iterator.hasNext()) {
            Tank tank = (Tank) iterator.next();
            System.out.println(tank.getDirection());
            System.out.println(tank.getX());
            System.out.println(tank.getY());
            System.out.println(tank.getId());
            System.out.println();
        }
    }

    public void showShots(ArrayList shots) {
        Iterator iterator = shots.iterator();

        System.out.println("Shots:");
        while (iterator.hasNext()) {
            Shot shot = (Shot) iterator.next();
            System.out.println(shot.getDirection());
            System.out.println(shot.getX());
            System.out.println(shot.getY());
            System.out.println();
        }
    }

    public String getStr() {
        return "";
    }

    public static String getKbdString() {
        byte bKbd[] = new byte[256];
        int iCnt = 0;
        String szStr = "";

        try {
            iCnt = System.in.read(bKbd);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        szStr = new String(bKbd, 0, iCnt);
        szStr = szStr.trim();

        return szStr;
    }
}